﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema.Admin
{
    public partial class QuanLyPhongChieu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                laydsphongchieu();
        }
        public void laydsphongchieu()
        {
            string str;
            string s, ma;
            DataTable ds_pc;
            ma = "@maphong";
            s = "SP_TIMKIEM_PHONGCHIEU_MAPHONG";
            ds_pc = Timkiem_MA("", s, ma);
            for (int i = 0; i < ds_pc.Rows.Count; i++)
            {
                str = ds_pc.Rows[i]["MaPhong"].ToString();
                DDLMaPhong.Items.Add(str);
            }
            ma = "@malp";
            s = "SP_TIMKIEM_LOAIPHONGCHIEU_MALP";
            ds_pc = Timkiem_MA("", s, ma);
            for (int j = 0; j < ds_pc.Rows.Count; j++)
            {
                str = ds_pc.Rows[j]["Malp"].ToString();
                DDLMaLoaiPhong.Items.Add(str);
            }
            ma = "@marap";
            s = "SP_TIMKIEM_RAPPHIM_MARAP";
            ds_pc = Timkiem_MA("", s, ma);
            for (int t = 0; t < ds_pc.Rows.Count; t++)
            {
                str = ds_pc.Rows[t]["Marap"].ToString();
                DDLMaRap.Items.Add(str);
            }
            DDLTinhTrang.Items.Add("Tốt");
            DDLTinhTrang.Items.Add("Đang Sửa Chữa");
            s = "SP_TIMKIEM_PHONGCHIEU_MAPHONG";
            ma = "@maphong";
            ds_pc = Timkiem_MA("", s, ma);
            grPhongChieu.DataSource = ds_pc;
            grPhongChieu.DataBind();
        }

        protected void DDLMaPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            string s = "SP_TIMKIEM_PHONGCHIEU_MAPHONG";
            string ma = "@maphong";
            DataTable dt = Timkiem_MA(DDLMaPhong.SelectedItem.ToString(), s, ma);
            if (dt.Rows.Count != 0)
            {
                DDLMaLoaiPhong.Text = dt.Rows[0][2].ToString();
                DDLMaRap.Text = dt.Rows[0][3].ToString();
                txtMoTa.Text = dt.Rows[0][1].ToString();
                DDLTinhTrang.Text = dt.Rows[0][4].ToString();
            }
        }

        protected void grPhongChieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLMaPhong.Text = grPhongChieu.SelectedRow.Cells[1].Text;
            DDLMaLoaiPhong.Text = grPhongChieu.SelectedRow.Cells[3].Text;
            DDLMaRap.Text = grPhongChieu.SelectedRow.Cells[4].Text;
            if (grPhongChieu.SelectedRow.Cells[2].Text == "&nbsp;")
                txtMoTa.Text = null;
            else
                txtMoTa.Text = grPhongChieu.SelectedRow.Cells[2].Text;
            DDLTinhTrang.Text = grPhongChieu.SelectedRow.Cells[5].Text;
        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@mota", txtMoTa.Text.ToString()));
                sqlPara.Add(new SqlParameter("@malp", DDLMaLoaiPhong.Text.ToString()));
                sqlPara.Add(new SqlParameter("@marap", DDLMaRap.Text.ToString()));
                sqlPara.Add(new SqlParameter("@tinhtrang", DDLTinhTrang.Text.ToString()));
                CDatastore.ThucHienStore("SP_INSERT_PHONGCHIEU", sqlPara);
                lblThongBao.Text = "Bạn đã thêm Ca Chiếu thành công";
                DDLMaPhong.Items.Clear();
                DDLMaLoaiPhong.Items.Clear();
                DDLMaRap.Items.Clear();
                DDLTinhTrang.Items.Clear();
                laydsphongchieu();
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void imXoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@maphong", DDLMaPhong.Text.ToString()));
                CDatastore.ThucHienStore("SP_DELETE_PHONGCHIEU", sqlPara);
                lblThongBao.Text = "Bạn đã xóa Mã Phòng Chiếu " + DDLMaPhong.Text + " thành công";
                DDLMaPhong.Items.Clear();
                laydsphongchieu();
                txtMoTa.Text = null;
                DDLMaLoaiPhong.Text = null;
                DDLMaRap.Text = null;
                DDLTinhTrang.Text = null;

            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void imSua_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@maphong", DDLMaPhong.Text.ToString()));
                sqlPara.Add(new SqlParameter("@mota", txtMoTa.Text.ToString()));
                sqlPara.Add(new SqlParameter("@malp", DDLMaLoaiPhong.Text.ToString()));
                sqlPara.Add(new SqlParameter("@marap", DDLMaRap.Text.ToString()));
                sqlPara.Add(new SqlParameter("@tinhtrang", DDLTinhTrang.Text.ToString()));
                CDatastore.ThucHienStore("SP_UPDATE_PHONGCHIEU", sqlPara);
                lblThongBao.Text = "Bạn đã cập nhật Mã Phong Chiếu " + DDLMaPhong.Text + " thành công";
                DDLMaPhong.Items.Clear();
                DDLMaLoaiPhong.Items.Clear();
                DDLMaRap.Items.Clear();
                DDLTinhTrang.Items.Clear();
                laydsphongchieu();
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        public DataTable Timkiem_MA(string ten, string str, string ma)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter(ma, ten));
                    return CDatastore.DocStore(str, sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore(str);
        }

        protected void grPhongChieu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grPhongChieu.PageIndex = e.NewPageIndex;
            grPhongChieu.DataBind();
            laydsphongchieu();
        }



        }
    }
